const BaseController = require("./base");
const NotFoundException = require("../exception/not_found");

function toInt(str) {
  if (typeof str === "number") return str;
  if (!str) return str;
  return parseInt(str, 10) || 0;
}

class ProgressController extends BaseController {
  async setProgress() {
    const { ctx, app } = this;
    const data = await ctx.service.progress.setProgress(ctx.request.body);
    this.success(data);
  }

  // 获取当天学习的单词数量
  async getTodayProgressCount() {
    const { ctx, app } = this;
    const data = await ctx.service.progress.getTodayProgressCount();
    this.success(data);
  }
  // 获取学习的单词数量
  async getProgressCount() {
    const { ctx, app } = this;
    const todayLearned = await ctx.service.progress.getTodayProgressCount();
    const totalLearned = await ctx.service.word.getLearnedWordCount();
    const total = await ctx.service.word.getWordCount();
    this.success({
      todayLearned,
      totalLearned,
      progress: ((totalLearned / total) * 100).toFixed(2),
    });
  }

  async updateReviewTime() {
    const { ctx, app } = this;
    const data = await ctx.service.progress.updateReviewTime(ctx.request.body);
    this.success(data);
  }
}

module.exports = ProgressController;
